package com.example.myapplication.ContentTest;

import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.os.CancellationSignal;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class VocabularyProvider extends ContentProvider {
  //定义URI和数据库相关的常量，在自定义的ContentProvider中必须要固定实现
  private static final String AUTHORITY =
      "com.example.myapplication.ContentTest.vocabularyProvider";
  private static final String BASE_PATH = "vocabulary";
  public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + BASE_PATH);

  private static final String CONTENT_TYPE =
      ContentResolver.CURSOR_DIR_BASE_TYPE + "/vnd.com.example.vocab";
  private static final String CONTENT_ITEM_TYPE =
      ContentResolver.CURSOR_ITEM_BASE_TYPE + "/vnd.com.example.vocab";

  //定义数据库和表的常量
  private static final String DATABASE_NAME = "vocabulary.db";
  private static final int DATABASE_VERSION = 1;
  public static final String COLUMN_WORD = "word";
  public static final String COLUMN_ILLUSTRATION = "illustration";

  // ...

  //数据库的帮助类
  private DicitionaryDataHelper dicitionaryDataHelper;

  @Override public boolean onCreate() {
    dicitionaryDataHelper = new DicitionaryDataHelper(getContext());
    return true;
  }

  //查询数据

  @Nullable @Override
  public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection,
      @Nullable String[] selectionArgs, @Nullable String sortOrder) {
    SQLiteDatabase db = dicitionaryDataHelper.getReadableDatabase();
    Cursor cursor =
        db.query(dicitionaryDataHelper.getTableName(), projection, selection, selectionArgs, null,
            null, sortOrder);
    cursor.setNotificationUri(getContext().getContentResolver(), uri);
    return cursor;
  }

  //根据MIME类型返回数据类型

  @Nullable @Override public String getType(@NonNull Uri uri) {
    return CONTENT_TYPE;
  }

  //插入数据

  @Nullable @Override public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
    SQLiteDatabase db = dicitionaryDataHelper.getWritableDatabase();
    long id = db.insert(dicitionaryDataHelper.getTableName(), null, values);
    getContext().getContentResolver().notifyChange(uri, null);
    return ContentUris.withAppendedId(CONTENT_URI, id);

  }

  @Override
  public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection,
      @Nullable String[] selectionArgs) {
    SQLiteDatabase db = dicitionaryDataHelper.getWritableDatabase();
    int count = db.update(dicitionaryDataHelper.getTableName(), values, selection, selectionArgs);
    getContext().getContentResolver().notifyChange(uri, null);
    return count;
  }

  @Override public int delete(@NonNull Uri uri, @Nullable String selection,
      @Nullable String[] selectionArgs) {
    SQLiteDatabase db = dicitionaryDataHelper.getWritableDatabase();
    int count = db.delete(dicitionaryDataHelper.getTableName(), selection, selectionArgs);
    getContext().getContentResolver().notifyChange(uri, null);
    return count;
  }
}
